class Solution {
public:
struct Cmp
{
    bool operator()(pair<string,int>& left,pair<string,int>& right)
    {
        return left.second>right.second||(left.second==right.second&&left.first<right.first);
    }
};
    vector<string> topKFrequent(vector<string>& words, int k) {
        //先统计次数
        map<string,int> countmap;
        for(auto& e:words)
        {
            countmap[e]++;
        }
        //
        vector<pair<string,int>> v(countmap.begin(),countmap.end());
        sort(v.begin(),v.end(),Cmp());
        vector<string> ret;
        for(int j=0;j<k;j++)
        {
            ret.push_back(v[j].first);
        }
        return ret;

    }
};